package game;

import java.io.IOException;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

/**
 * exploding walls (normal walls get replaced by this object when exploding)
 * @author Simon
 *
 */
public class ExplodingWall implements ArenaObject {

	public static final int ANIM_DELAY = 4;
	
	int tile_x, tile_y;
	
	static Image image = null;
	Sprite gfx = null;
	
	int[] animFrame = {13, 14, 15, 16, 17};
	int currentFrame;
	int frameDelay;
	boolean dead = false;
	
	public ExplodingWall(int tile_x, int tile_y)
	{
		this.tile_x = tile_x;
		this.tile_y = tile_y;
		
		try {
			if (image == null) {
				image = Image.createImage("/gfx/tiles.png");
			}
			gfx = new Sprite(image, 20, 20);
			Arena.Position(gfx, tile_x, tile_y);
			currentFrame = 0;
			gfx.setFrame(animFrame[currentFrame]);
			frameDelay = ANIM_DELAY;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void advance() {
		if (dead) return;
		frameDelay--;
		if (frameDelay <= 0) {
			currentFrame++;
			frameDelay = ANIM_DELAY;
			if (currentFrame >= animFrame.length) {
				dead = true;
			} else {
				gfx.setFrame(animFrame[currentFrame]);
			}
		}
	}

	public void draw(Graphics g) {
		if (!dead) gfx.paint(g);
	}

	public boolean isDead() {
		return dead;
	}

}
